package com.zdw.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alicp.jetcache.anno.*;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zdw.common.constant.jetcache.JetCacheConst;
import com.zdw.demo.dto.CustomerSelectDto;
import com.zdw.demo.entity.CustomerEntity;
import com.zdw.demo.mapper.CustomerMapper;
import com.zdw.demo.service.CustomerService;
import com.zdw.demo.dto.CustomerAddDto;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 卓德文
 * @since 2023-04-05
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, CustomerEntity> implements CustomerService {

    /**
     * 新增客户信息
     *
     * @param customerAddDto
     * @return
     */
    @Override
    @DS("master")
    public int save(CustomerAddDto customerAddDto) {
        CustomerEntity customerEntity = BeanUtil.copyProperties(customerAddDto, CustomerEntity.class);
        return this.baseMapper.insert(customerEntity);
    }

    /**
     * 修改客户信息
     *
     * @param customerEntity
     * @return
     */
    @Override
    @DS("master")
    //缓存更新注解，用于方法上(在满足condition的条件下,将缓存进行更新)
    //area:缓存的前缀,name:缓存的名称,key:缓存的唯一键,最终的键名为:[area_]+name+key
    //value:更新的内容,必须在入参中存在
    @CacheUpdate(
            area = JetCacheConst.CACHE_AREA_DEMO, name = "customer_", key = "#customerEntity.id",
            value = "#customerEntity"
    )
    public int update(CustomerEntity customerEntity) {
        return this.baseMapper.updateById(customerEntity);
    }

    /**
     * 删除客户信息
     *
     * @param id
     * @return
     */
    @Override
    @DS("master")
    //缓存失效注解，用于方法上(在满足condition的条件下,将缓存删除)
    //area:缓存的前缀,name:缓存的名称,key:缓存的唯一键,最终的键名为:[area_]+name+key
    @CacheInvalidate(
            area = JetCacheConst.CACHE_AREA_DEMO, name = "customer_", key = "#id"
    )
    public int delete(int id) {
        return this.baseMapper.deleteById(id);
    }

    /**
     * 查询单个客户信息
     *
     * @param id
     * @return
     */
    @Override
    @DS("master")
    //指定未命中缓存的情况下，同一个 JVM 同一个 key 只有一个线程去操作,用于防止并发请求(缓存穿透)
    @CachePenetrationProtect
    //缓存刷新频率(默认单位:s)
    @CacheRefresh(refresh = 30, timeUnit = TimeUnit.MINUTES)
    //标记缓存注解，用于方法上，再满足condition的条件时创建一个缓存
    //area:缓存的前缀,name:缓存的名称,key:缓存的唯一键,最终的键名为:[area_]+name+key
    //cacheType:接口缓存的方式,有REMOTE[远程]、LOCAL[本地]、BOTH[全部]三种
    @Cached(
            area = JetCacheConst.CACHE_AREA_DEMO, name = "customer_", key = "#id",
            cacheType = CacheType.BOTH, expire = 60, localExpire = 5, timeUnit = TimeUnit.MINUTES
    )
    public CustomerEntity selectOne(Integer id) {
        return this.baseMapper.selectById(id);
    }

    /**
     * 条件查询已启用的客户信息列表
     *
     * @param customerSelectDto
     * @return
     */
    @Override
    @DS("master")
    public List<CustomerEntity> selectList(CustomerSelectDto customerSelectDto) {
        LambdaQueryWrapper<CustomerEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CustomerEntity::getState, 0)
                .like(StrUtil.isNotBlank(customerSelectDto.getName()), CustomerEntity::getName, customerSelectDto.getName());
        return this.baseMapper.selectList(queryWrapper);
    }

    /**
     * 客户信息分页查询
     *
     * @param customerSelectDto
     * @return
     */
    @Override
    @DS("master")
    public IPage<CustomerEntity> page(CustomerSelectDto customerSelectDto) {
        IPage<CustomerEntity> page = new Page<>(customerSelectDto.getPageNum(), customerSelectDto.getPageSize());
        Wrapper<CustomerEntity> wrapper = Wrappers.lambdaQuery(CustomerEntity.class)
                .like(StrUtil.isNotBlank(customerSelectDto.getName()), CustomerEntity::getName, customerSelectDto.getName())
                .eq(CustomerEntity::getState, 0);
        return this.baseMapper.selectPage(page, wrapper);
    }

}
